package cn.yup.web.controller;

import cn.yup.domain.DetailPosition;
import cn.yup.domain.Sample;
import cn.yup.domain.Work;

import cn.yup.query.QueryObject;
import cn.yup.query.SampleQueryObject;
import cn.yup.service.IDetailPositionService;
import cn.yup.service.ISampleService;
import com.github.pagehelper.PageInfo;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller // 标识一下 可以被扫描 创建对象 加入spring容器
@RequestMapping("/sample") // 访问路径的映射地址   作用 ： 可以通过浏览器 进行访问的地址
public class SampleController {


    @Autowired// 自动装配  SampleService
    private ISampleService sampleService;

    @Autowired
    private IDetailPositionService detailPositionService;

    // 想让浏览器直接访问的
    @RequestMapping("/list")  // 类上面的requestMapping的内容 + 方法上面的requestMapping的内容
    public String list(Model model, @ModelAttribute("qo") SampleQueryObject qo) {  // 返回值是String   返回值的内容 一般是一个地址
        PageInfo pageInfo = sampleService.findAll(qo);
        // 把数据共享给页面
        model.addAttribute("pageInfo", pageInfo);
        return "/sample/list";
    }

    @RequestMapping("/importXls")
    public String importXls(MultipartFile multipartFile) throws IOException {

        //获取第一个sheet
        Workbook workbook = WorkbookFactory.create(multipartFile.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);

        //获取最后一行
        int lastRowNum = sheet.getLastRowNum();

        List<DetailPosition> detailPositions = new ArrayList<>();
        List<Sample> samples = new ArrayList<>();
        
        for(int i=3;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            if(i>3 && row.getCell(0).toString().equals("")){
                break;
            }
            Sample sample = new Sample();
            String sampleNum = row.getCell(1).getStringCellValue();
            String surveryName = row.getCell(2).getStringCellValue();
            String voyageNum = row.getCell(3).getStringCellValue();

            //封装里面的对象
            DetailPosition detailPosition = new DetailPosition();
            String seaAreaName = row.getCell(4).getStringCellValue();
            String detailStation = row.getCell(5).getStringCellValue();
            BigDecimal positionX = BigDecimal.valueOf(row.getCell(6).getNumericCellValue());
            BigDecimal positionY = BigDecimal.valueOf(row.getCell(7).getNumericCellValue());
            String detailAddress = row.getCell(8).getStringCellValue();
            detailPosition.setSeaAreaName(seaAreaName);
            detailPosition.setDetailStation(detailStation);
            detailPosition.setPositionX(positionX);
            detailPosition.setPositionY(positionY);
            detailPosition.setDetailAddress(detailAddress);
            detailPositions.add(detailPosition);

            BigDecimal heightEnd = BigDecimal.valueOf(row.getCell(9).getNumericCellValue());
            BigDecimal cardiomegaly = BigDecimal.valueOf(row.getCell(10).getNumericCellValue());
            String savePosition = row.getCell(11).getStringCellValue();
            BigDecimal saveSolution = BigDecimal.valueOf(row.getCell(12).getNumericCellValue());
            String comment = row.getCell(13).getStringCellValue();

            sample.setSampleNum(sampleNum);
            sample.setSurveryName(surveryName);
            sample.setVoyageNum(voyageNum);
            sample.setHeightEnd(heightEnd);
            sample.setCardiomegaly(cardiomegaly);
            sample.setSavePosition(savePosition);
            sample.setSaveSolution(saveSolution);
            sample.setComment(comment);
            sample.setDetailPosition(detailPosition);
            samples.add(sample);
        }

        //插入子表
        detailPositionService.insertDetailPositions(detailPositions);
        sampleService.insertSamples(samples);

        return "redirect:/sample/list";

    }

}





